package controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import models.Offer;
import models.Service;
import models.User;
import service.NotificationManager;
import service.SearchManager;
import service.bingosearch.IndexManager;

public class BingoSearch extends BaseController {

    public static void rebuildIndex(Long id) throws Exception
    {
        IndexManager indexManager = new IndexManager();
        String result = "BingoSearch index rebuilt successfully";
        
        try {
        	indexManager.BuildSearchIndexes();
        	
		} catch (Exception e) {
			result = "Problem occured in reindexing operations.";
		}
        
        render(result);
    }
    
    public static void results(String query, String type) throws IOException
    {
    	SearchManager searchManager = new SearchManager();
    	
    	List<Long> excludedUsers = new ArrayList<Long>();
    	Boolean isOffer = null;
    	if (type != null){
    		isOffer = type.equals("Offer");
        	if (type.equals("All")) isOffer = null;
    	}
    	List<Service> services = searchManager.SearchServices(query, excludedUsers, isOffer);
    	
    	// filter services (sessions will be excluded).
    	List<Service> filteredServices = new ArrayList<Service>();
    	for (Service service : services) {
    		// bad hack.
    		Service service2 = Service.findById(service.id);
    		if (null == service2.parent) {
    			filteredServices.add(service2);
    		}
    	}
    	
    	render(query, filteredServices, type);
    }
	
}
